var _LO = (function() {
    this.controlset=null;
    var oTable,onInit=false;;
    return {
        onInit  :   function(){
            if(onInit) return;onInit=true;
            _CONFIG.CATEFILLTER='';
            _CONFIG.tBINDING_URI=base_url + 'so/news/binding/';
            _CONFIG.cBINDING_URI= base_url + 'so/category/gets';
            if(_CONFIG.CATEGORYS===undefined || _CONFIG.CATEGORYS===null ){
                backend({
                    url         :   _CONFIG.cBINDING_URI,
                    data        :   {
                    },
                    callback    :   function(rsdata){
                        if(rsdata.result<0){
                            bckdialog({type:'error'}).open(rsdata.message);
                        }else{
                            _CONFIG.CATEGORYS=rsdata.data;
                            _LO.create_tree_cate();
                        }
                    }
                }).call();
            }else{
                _LO.create_tree_cate();
            }
            _LO.createtable();
        },
        onChangeSource:function(cats){
            _CONFIG.CATEFILLTER=cats;
            oTable.fnDestroy();
            this.createtable();
        },
        createtable :   function(){
             oTable = $('#news-link-pop').dataTable({
                aaSorting: [],
                aoColumns: [
                    {
                        mData: "Id",sWidth: "58px",//bVisible:false
                    },
                    {
                        mData: "Thumb",sWidth: "58px",bVisible:false,
                        mRender: function ( value, type, datarow ) {
                            var str = "";
                            if(value!==undefined && value !==null  && value!=='')
                                str += '<img src="' + value + '" style="height:24px"/>';
                            return str;
                        }
                    },
                    {
                        mData: "Title",sClass:'rnode',
                        mRender: function ( value, type, datarow ) {
                            var str='';
                            try{
                                var avalue = datarow.CategoryId.split(',');
                                for (var i = 0; i < avalue.length; i++) {
                                    var display = avalue[i];
                                    try{
                                        for(var j=0;j<_CONFIG.CATEGORYS.length;j++){
                                            if(avalue[i]==_CONFIG.CATEGORYS[j].Id){
                                                display = _CONFIG.CATEGORYS[j].Title;
                                                display+='<sup>('+_CONFIG.CATEGORYS[j].Type+')</sup>';
                                                break;
                                            }
                                        }
                                    }catch(e){}
                                    str += display;
                                    if(i+1 < avalue.length){
                                        str+=', ';
                                    }
                                }
                            }catch(e){str=datarow.CategoryId}
                        return '<b>'+value+
                                    '</b> - <span>'+str+'</span><div><i>'+datarow.Insert+'</i></div><div class="act">\n\
                                        <span onclick="_LO.onSetNewsLink(this)"\n\
                                            data-newsid="'+datarow.Id+'" \n\
                                            data-newsalias="'+datarow.Alias+'"\n\
                                            title="Get link" class="icon-link" ></span>\n\
                                    </div>';
                        }
                    },
                    {mData: "Alias",bVisible:false},
                    {mData: "CategoryId",bVisible:false},
                    {mData: "Insert",bVisible:false,bSearchable:false},
                    {mData: "Update",bVisible:false,bSearchable:false}
                ],
                sServerMethod: "POST",
                iDisplayLength: 10,
                bProcessing: true,
                bServerSide: true,
                sScrollY: "260px",
                //bPaginate: false,
                //bSortable:false,
                //bFilter :false,
                sAjaxSource: _CONFIG.tBINDING_URI+_CONFIG.CATEFILLTER,
                bJQueryUI: false,
                bAutoWidth: false,
                sPaginationType: "full_numbers",
                //sDom": '<"datatable-header"fl>t<"datatable-footer"ip>',
                oLanguage: {
                    sSearch: "<span>Filter records:</span> _INPUT_",
                    sLengthMenu: "<span>Show entries:</span> _MENU_",
                    oPaginate: {"sFirst": "First", "sLast": "Last", "sNext": ">", "sPrevious": "<"}
                }
            });
            oTable.fnSetFilteringDelay(2000);
        },
        onOpenPopup:   function(control){
            this.controlset=control;
            if($('#link-choose').length===0){
                $('body').append('<div id="link-choose">\n\
                <div class="" style="width: 720px;position: relative;min-height: 320px;border: 1px solid #ccc">\
                    <div style="border-right:1px solid #ccc;width:220px;position: absolute;top: 0;left: 0;height: 100%;\
                         overflow-x: auto;" >    \
                        <div id="category-tree" style="padding: 4px"></div>\
                    </div>\
                    <div style="padding-left: 225px;overflow-x: auto;min-height: 320px;">\
                        <div class="table-overflow hidden-thead" style="border-right: 0;border-bottom: 0">\
                            <table class="table table-striped table-bordered" id="news-link-pop">       \
                                <thead>\
                                    <tr>\
                                        <th>&nbsp;</th>\
                                        <th>Thumb</th>\
                                        <th>Title</th>\
                                        <th>Alias</th>\
                                        <th>Category</th>\
                                        <th>Created</th>\
                                        <th>Modified</th>\
                                    </tr>\
                                </thead>\
                                <tbody>\
                                </tbody>\
                            </table>\
                        </div>\
                    </div>\
                </div></div>');
            }
            bckdialog({
                title:'Link',
                message:$('#link-choose'),
                buttons: [
                    {
                        text    :'Cancel',
                        class   :'base_button base_red base-small-border-radius',
                        click   : function() {
                            $(this).dialog("close");
                        }
                    }
                ]
            }).open();
            _LO.onInit();
        },
        onClose:function(){
            $('#link-choose').dialog("close");
        },
        onSetNewsLink:function(that){
            var newsid=$(that).data('newsid');
            var newsalias=$(that).data('newsalias');
            if(this.controlset!==null)
                $(this.controlset).val('/bai-viet/'+newsid+'-'+newsalias);
            this.onClose();
        },
        onSetCateLink:function(that){
            var cateid=$(that).parents('.cnode').data('cateid');
            var catealias=$(that).parents('.cnode').data('catealias');
            if(this.controlset!==null)
                $(this.controlset).val('/danh-muc/'+cateid+'-'+catealias);
            this.onClose();
        },
        onLoadNews:function(that){
            $('#category-tree .cnode.active').removeClass('active');
            $(that).parents('.cnode').addClass('active');
            var $nodes =$('#category-tree .cnode');
            var lv=$(that).parents('.cnode').data('catelevel');
            var id=$(that).parents('.cnode').data('cateid');
            var elm=$(that).parents('.cnode');
            var index = $nodes.index(elm);
            var arr=[id];
            for(var i=index+1;i<$nodes.length;i++){
                var $nlv = parseInt($($nodes[i]).data('catelevel'));
                if( $nlv > parseInt(lv)){
                    arr.push($($nodes[i]).data('cateid'));
                }else{
                    break;
                }
            }
            this.onChangeSource(arr.join('-'));
        },
        create_tree_cate:function(){
            var $tree =$('#category-tree');
            $tree.html('<div class="base-group-title" style="cursor:pointer" onclick="_LO.onChangeSource(\'\')">ALL - NEWS LIST</div>');
            $tree.append('<div  class="base-group-title">WEB</div>');
            $.each(_CONFIG.CATEGORYS, function( index, value ) {
                if(value.Type==='Web'){
                    var padding=parseInt(value.Level)*16+16;
                    var $node=' <div class="cnode" style="padding-left:'+padding+'px"\n\
                                    data-cateid="'+value.Id+'" \n\
                                    data-catelevel="'+value.Level+'"\n\
                                    data-catealias="'+value.Alias+'"\n\
                                >\n\
                                <div><span class="node-name" onclick="_LO.onLoadNews(this)">'+value.Title+'</span>\
                                    <div class="act">\n\
                                        <span title="Open news list" onclick="_LO.onLoadNews(this)" class="icon-list-alt"></span>\n\
                                        <span title="Get link" class="icon-link" onclick="_LO.onSetCateLink(this)"></span>\n\
                                    </div>\n\
                                </div>\n\
                            </div>';
                    $tree.append($node);
                }
            });
            $tree.append('<div class="base-group-title">SWAP</div>');
            $.each(_CONFIG.CATEGORYS, function( index, value ) {
                if(value.Type==='Mobile'){
                    var padding=parseInt(value.Level)*16+32;
                    var $node=' <div class="cnode" style="padding-left:'+padding+'px"\n\
                                    data-cateid="'+value.Id+'" \n\
                                    data-catelevel="'+value.Level+'"\n\
                                    data-catealias="'+value.Alias+'"\n\
                                >\n\
                                <div><span class="node-name" onclick="_LO.onLoadNews(this)">'+value.Title+'</span>\
                                    <div class="act">\n\
                                        <span title="Open news list" onclick="_LO.onLoadNews(this)" class="icon-list-alt"></span>\n\
                                        <span title="Get link" class="icon-link"></span>\n\
                                    </div>\n\
                                </div>\n\
                            </div>';
                    $tree.append($node);
                }
            });
            $tree.append('<div class="base-group-title">WAP</div>');
            $.each(_CONFIG.CATEGORYS, function( index, value ) {
                if(value.Type==='Wap'){
                    var padding=parseInt(value.Level)*16+16;
                    var $node=' <div class="cnode" style="padding-left:'+padding+'px"\n\
                                    data-cateid="'+value.Id+'" \n\
                                    data-catelevel="'+value.Level+'"\n\
                                    data-catealias="'+value.Alias+'"\n\
                                >\n\
                                <div><span class="node-name" onclick="_LO.onLoadNews(this)">'+value.Title+'</span>\
                                    <div class="act">\n\
                                        <span title="Open news list" onclick="_LO.onLoadNews(this)" class="icon-list-alt"></span>\n\
                                        <span title="Get link" class="icon-link"></span>\n\
                                    </div>\n\
                                </div>\n\
                            </div>';
                    $tree.append($node);
                }
            });
        }
    };
}());